package cs3343.project.family.UI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.ProfileManager;

/**
 * The Class ProfileCreateFrame.
 */
public class ProfileCreateFrame extends JFrame implements ActionListener {

	/** The gc. */
	private GameController gc;
	
	/** The content pane. */
	private JPanel contentPane;
	
	/** The txt_name. */
	private JTextField txt_name;
	
	/** The txt_role. */
	private JTextField txt_role;
	
	/** The rdbtn male. */
	private JRadioButton rdbtnMale;
	
	/** The rdbtn female. */
	private JRadioButton rdbtnFemale;

	/**
	 * Create the frame.
	 *
	 * @param gc the gc
	 */
	public ProfileCreateFrame(GameController gc) {
		this.gc = gc;
		setTitle(ConfigManager.getLangString("profileCrateTitle"));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 400, 150);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblCreateNewProfile = new JLabel(ConfigManager.getLangString("lblCreateNewProfile"));
		GridBagConstraints gbc_lblCreateNewProfile = new GridBagConstraints();
		gbc_lblCreateNewProfile.gridwidth = 7;
		gbc_lblCreateNewProfile.insets = new Insets(0, 0, 5, 0);
		gbc_lblCreateNewProfile.gridx = 0;
		gbc_lblCreateNewProfile.gridy = 0;
		contentPane.add(lblCreateNewProfile, gbc_lblCreateNewProfile);
		
		JLabel lbl_name = new JLabel(ConfigManager.getLangString("lblName"));
		GridBagConstraints gbc_lbl_name = new GridBagConstraints();
		gbc_lbl_name.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_name.gridx = 0;
		gbc_lbl_name.gridy = 2;
		contentPane.add(lbl_name, gbc_lbl_name);
		
		txt_name = new JTextField();
		GridBagConstraints gbc_txt_name = new GridBagConstraints();
		gbc_txt_name.gridwidth = 4;
		gbc_txt_name.insets = new Insets(0, 0, 5, 5);
		gbc_txt_name.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_name.gridx = 2;
		gbc_txt_name.gridy = 2;
		contentPane.add(txt_name, gbc_txt_name);
		txt_name.setColumns(10);
		
		JButton btn_create = new JButton(ConfigManager.getLangString("btnCreate"));
		btn_create.addActionListener(this);
		
		GridBagConstraints gbc_btn_create = new GridBagConstraints();
		gbc_btn_create.insets = new Insets(0, 0, 0, 5);
		gbc_btn_create.gridx = 4;
		gbc_btn_create.gridy = 10;
		contentPane.add(btn_create, gbc_btn_create);
		
		gc.getUiController().frameCentering(this);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (!submitForm())
			JOptionPane.showMessageDialog(null, ConfigManager.getLangString("msgFailCreate"), ConfigManager.getLangString("errorTitle"), JOptionPane.ERROR_MESSAGE);
		else
			JOptionPane.showMessageDialog(null, ConfigManager.getLangString("msgCreated"));
	}
	
	/**
	 * Submit form.
	 *
	 * @return true, if successful
	 */
	private boolean submitForm() {		
		if (!txt_name.getText().isEmpty())
			ProfileManager.createNewProfile(txt_name.getText());
		else
			return false;
		return true;
	}

}
